package com.foxzzz.operation;

import com.foxzzz.database.SqlExecutor;

import java.util.List;

public class SysConfigDataBaseOperation {
    public final static String INSERT_INTO_SYS_CONFIG="INSERT INTO SYS_CONFIG VALUES(?,?)";
    public final static String UPDATE_SYS_CONFIG="UPDATE SYS_CONFIG SET VAl=? WHERE KEY=?";
    public final static String QUERY_SYS_CONFIG="SELECT VAL FROM SYS_CONFIG=?";
    public final static String DELETE_SYS_CONFIG="DELETE FROM SYS_CONFIG WHERE KEY=?";
    public final static String CLEAR_SYS_CONFIG="DELETE FROM SYS_CONFIG";

    public String upsert(String key,String val){
        List l= SqlExecutor.queryBySql(QUERY_SYS_CONFIG,key);
        if(l.size()==0)
            SqlExecutor.updateBySql(INSERT_INTO_SYS_CONFIG,key,val);
        else
            SqlExecutor.updateBySql(UPDATE_SYS_CONFIG,val,key);
        return Msg.SUCCESS;
    }

    public String reset(String key){
        SqlExecutor.updateBySql(DELETE_SYS_CONFIG,key);
        return Msg.SUCCESS;
    }

    public String resetAll(){
        SqlExecutor.updateBySql(CLEAR_SYS_CONFIG);
        return Msg.SUCCESS;
    }
}
